package com.basic.library.base;

import android.view.View;
import android.view.ViewGroup;

import com.trello.rxlifecycle2.components.support.RxFragment;

import java.util.Calendar;

/**
 * @author jiangyun
 * @description
 * @time 创建时间：2017/11/22 16:00
 * 解决重复点击
 */
public abstract class NoDoubleClickFragment extends RxFragment implements View.OnClickListener {

    public static int MIN_CLICK_DELAY_TIME = 600;// 这里设置不能超过多长时间
    private long lastClickTime = 0;

    protected abstract void onNoDoubleClick(View v);

    public NoDoubleClickFragment() {
    }

    public NoDoubleClickFragment(int time) {
        MIN_CLICK_DELAY_TIME = time;
    }

    @Override
    public void onClick(View v) {
        long currentTime = Calendar.getInstance().getTimeInMillis();
        if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME) {
            lastClickTime = currentTime;
            onNoDoubleClick(v);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        //主要为了去掉EditText的焦点，避免页面回到前台时自动弹出软键盘，
        try {
            View view = ((ViewGroup) getActivity().findViewById(android.R.id.content));
            if (view != null) {
                view.setFocusable(true);
                view.setFocusableInTouchMode(true);
                view.requestFocus();
            } else {
                //这种方法要给布局文件手动加，setFocusable，setFocusableInTouchMode无效
                //            android:focusable="true"
                //            android:focusableInTouchMode="true"
                //        getWindow().getDecorView().setFocusable(true);
                //        getWindow().getDecorView().setFocusableInTouchMode(true);
                getActivity().getWindow().getDecorView().requestFocus();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
